<?php
//Bring product arrays
include 'product_data.inc';

//define functions
require_once 'functions.inc';
$all_users=  arrayfile_to_array('./user_data.dat');
$username=$_POST['username'];

//invoice table ?>
<?php 
//security
if(array_key_exists($username, $all_users)){

print "This is your shopping cart, ". $_POST['username'] . ".";
print "<br>";
print "<font size= 5><b>INVOICE</b>";
print "<br>";
print "<table border= '1' cellpadding= '5' cellspacing= '0'>";
print '<th>Name</th> <th>Quantity Ordered</th> <th>Price Per Item</th> <th>Extended Price</th>';
        
//This starts the invoice total as $0 and then adds to the total.
$total = 0;
$qty_ordered= 0;
for ($i= 0; $i < count($products); $i++) {
	$qty= @$_POST["quantity$i"];


	// This checks to see if the user inputted an integer.
	if ($qty> 0 && $qty- (int) $qty== 0) {
             
	// This calculates the price if the user orders more than 1 item.
	$extended_price = $qty*$products[$i]['Price'];

	// This calculates the total price before shipping and tax.
	$total = $total+$extended_price;
        
	// This displays the data I want in a table.
	print "<tr><td colspan= '1'>{$products[$i]['Name']}</td><td colspan= '1'>$qty</td>";
        print "<td colspan= '1'>";
            printf('$%.2f',"{$products[$i]['Price']}");
        print "</td>";
	print "<td colspan= '1'>";
            printf('$%.2f',($extended_price));
        print "</td></tr>";
	print "</table";       
        
        }
       
}

// Here, I want to calculate extra costs such as taxes.
// The tax in Hawaii is 4.72%.
$tax= .0472;
$tax_amount= $tax*$total;

	print "<tr><td colspan= '3'><b>Subtotal</b></td>";
	print "<td colspan= '1'>";
            //This is the correct format of prices.
            printf('$%.2f', $total);
        print "</td>";
	print "<tr><td colspan= '3'><b>Tax</b></td>";
	print "<td colspan= '1'>";
            printf('$%.2f',($tax_amount));
	print "</td>";
	print "<tr><td colspan='3'><b>Shipping</b></td>";


// I included different shipping rates.
// I believe that shipping costs influence buyers.
if($total > 0) $shipping = 10;
if($total > 100) $shipping = 25;
if($total > 200) $shipping = 0;
	print "<td colspan= '1'>";
            printf('$%.2f',$shipping);
        print "</td></tr>";


$subtotal= $total+$tax_amount+$shipping;

	print "<tr><td colspan='3'><b>Grand Total</b></td>";
	print "<td colspan= '1'>";
            printf('$%.2f',$subtotal);
        print "</td></tr>";
}        
?>